<?php

/**
 *   OPENSHOP 管理中心文章处理程序文件
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: fad_union.php,v 1.0 2009/06/17 08:36:58 modified $
 *   @author      FillBag <fillbag@hotmail.com>
 */

define('IN_OS', true);

require(dirname(__FILE__) . '/includes/init.php');

if(!isset($_REQUEST['act']) || empty($_REQUEST['act'])){
	$_REQUEST['act'] = 'list';
}
if($_REQUEST['act'] == 'list'){
	
	//检查权限
	admin_priv(); 
		
    $smarty->assign('ur_here',      '联盟用户列表');
    $smarty->assign('full_page',    1);

	$list = get_fadunion_userlist();
	$smarty->assign('user_list',    $list['fadunion']);
    
    $smarty->assign('filter',       $list['filter']);
    $smarty->assign('record_count', $list['record_count']);
    $smarty->assign('page_count',   $list['page_count']);

    assign_query_info();
    
    $smarty->display('fadunion_user_list.tpl');	
}elseif ($_REQUEST['act'] == 'query'){
    $list = get_fadunion_userlist();
    $smarty->assign('acl_role_list',  $list['fadunion']);
    $smarty->assign('filter',         $list['filter']);
    $smarty->assign('record_count',   $list['record_count']);
    $smarty->assign('page_count',     $list['page_count']);
    
    make_json_result($smarty->fetch('fadunion_user_list.tpl'), '',
        array('filter' => $list['filter'], 'page_count' => $list['page_count']));
}

function get_fadunion_userlist(){
    $result = get_filter();
    if ($result === false)
    {
        /* 过滤条件 */
        $filter['keywords'] = empty($_REQUEST['keywords']) ? '' : trim($_REQUEST['keywords']);
        if ($_REQUEST['is_ajax'] == 1)
        {
            $filter['keywords'] = json_str_iconv($filter['keywords']);
        }

        $filter['sort_by']    = empty($_REQUEST['sort_by'])    ? 'user_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC'     : trim($_REQUEST['sort_order']);

//        $ex_where = ' WHERE union_status=1 OR union_status=2 ';
        if ($filter['keywords'])
        {
            $ex_where .= " AND user_name LIKE '%" . mysql_like_quote($filter['keywords']) ."%'";
        }     

        $filter['record_count'] = $GLOBALS['db']->getOne("SELECT COUNT(*) FROM " . $GLOBALS['os']->table('users') . $ex_where);

        /* 分页大小 */
        $filter = page_and_size($filter);
        $sql = "SELECT user_id, user_name, email, sex, office_phone, home_phone, mobile_phone, reg_time ".
                " FROM " . $GLOBALS['os']->table('users') . $ex_where .
                " ORDER by " . $filter['sort_by'] . ' ' . $filter['sort_order'] .
                " LIMIT " . $filter['start'] . ',' . $filter['page_size'];

        $filter['keywords'] = stripslashes($filter['keywords']);
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }

    $user_list = $GLOBALS['db']->getAll($sql);

    $count = count($user_list);
    for ($i=0; $i<$count; $i++)
    {
 		$sex_list   =  array( 0 =>'保密', 1 => '男', 2 => '女');
    	$user_list[$i]['sex_format'] = in_array(intval($user_list[$i]['sex']), array(0, 1, 2)) ? $sex_list[$user_list[$i]['sex']] : 0;   
        $user_list[$i]['reg_time'] = local_date($GLOBALS['_CFG']['date_format'], $user_list[$i]['reg_time']);
    }

    $arr = array('fadunion' => $user_list, 'filter' => $filter,
        'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);

    return $arr;	
}
?>